package com.itcs.box.server.conf;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import com.itcs.box.BoxConstants;

/**
 * redis 发往通讯盒 消息订阅配置 验证通讯盒登录 消息订阅配置
 * 
 * @author LiuPeng
 * @date 2018年1月25日
 * 
 */
@Configuration
public class RedisTopicBoxConfig {

	@Autowired
	private TopicMsgReceiver topicMsgReceiver;

	// 初始化监听器
	@Bean
	public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
			MessageListenerAdapter listenerAdapter) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		List<ChannelTopic> topics = new ArrayList<>();
		topics.add(new ChannelTopic(BoxConstants.PUBSUB_BOX_CHANNEL));
		topics.add(new ChannelTopic(BoxConstants.PUBSUB_BOXID_CHECK));
		container.addMessageListener(listenerAdapter(), topics);
		return container;
	}

	@Bean
	public MessageListenerAdapter listenerAdapter() {
		return new MessageListenerAdapter(topicMsgReceiver, "topicMsgReceiver");
	}

}
